fix: preserve worktree with commits on force-shutdown - #168
Conversation
The exit handler registered for new worktrees would unconditionally delete the worktree when process.exit() was called before the normal preservation block could null out worktreeCleanup. This happened on: - Double Ctrl+C (force-stop) - Graceful-shutdown timeout (5s) - Orchestrator crash via die() Fix: the exit handler now checks orchestrator state before cleaning up. If commitCount > 0 or hasPendingCommitFailure is true, the worktree is preserved even when the normal code path was bypassed.
3780fe2 to
4070bdf
Compare
|
thanks for this. quick note on process - i require PRs to come through no-mistakes, which is why the "PR must be raised via no-mistakes" check is red here (this was opened directly). the reason i ask for it: no-mistakes runs review, tests, lint, and docs on your branch before the PR is opened, so contributions arrive already validated. that keeps the quality bar high and keeps me from having to hand-review every change - i maintain this solo, so it's the only way i keep up. can you re-raise it through no-mistakes? running the pipeline on your branch opens/updates the PR through the gate and turns that check green. the change looks useful, it just needs to come through that path. ping me if the setup gives you trouble. |
|
Speaking as Kun's firstmate: VISION.md per-rule:
Closing as superseded by merged #189. Thank you @caoergou — really appreciate you taking the time on this. |
Closes #167
Problem
When using
--worktree, if the process exits via force-shutdown (double Ctrl+C or graceful-shutdown timeout), the worktree directory is deleted even though it contains commits that should be preserved.Root cause: The
process.on("exit")handler at line ~769 unconditionally callsremoveWorktree()whenworktreeCleanuphasn't been nulled out. But the force-shutdown path (process.exit(130)at line ~1068) fires before the normal preservation block at line ~1158 can setworktreeCleanup = null.Fix
The exit handler now queries
orchestrator.getState()before deciding to clean up. IfcommitCount > 0orhasPendingCommitFailureis true, the worktree is preserved regardless of whether the normal code path ran.A
getOrchestratorStateclosure variable is introduced to give the early-registered exit handler access to the orchestrator instance that's created later.Changes
src/cli.ts: Exit handler checks orchestrator state; addedgetOrchestratorStatevariablesrc/cli.test.ts: New test verifying exit handler preserves worktree when commits existTest plan
npx tsc --noEmitpasses